package com.cf.controller;


import com.cf.entity.AccountTypeIdType;
import com.cf.entity.IdType;
import com.cf.entity.Result;
import com.cf.service.AccountTypeIdTypeService;
import com.cf.service.IdTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class IdTypeController {

    @Autowired
    private IdTypeService idTypeService;

    @Autowired
    private AccountTypeIdTypeService accountTypeIdTypeService;

    @RequestMapping("/selectAllIdType")
    public Object selectAllIdType(@RequestParam(defaultValue = "1") int pagenum, int pagesize, String name){
        Map map = new HashMap<>();
        map.put("name",name);
        PageInfo<IdType> pageInfo = idTypeService.selectAllIdType(pagenum, pagesize, map);
            if (pageInfo.getList().size()!=0){
            return new Result(200,"查询成功",pageInfo);
        }
        return new Result(400,"没有数据",null);
    }


    @RequestMapping("/updateIdType")
    public Object updateIdType(IdType idType){
        int num = idTypeService.updateIdType(idType);
        if (num!=0){
            return new Result(200,"修改成功",null);
        }
        return new Result(400,"修改失败",null);
    }


    @RequestMapping("/addIdType")
    public Object addIdType(IdType idType){
        int num = idTypeService.addIdType(idType);
        if (num!=0){
            return new Result(200,"添加成功",null);
        }
        return new Result(400,"添加失败",null);
    }

    @RequestMapping("/delIdType")
    public Object delIdType(long id){

        List<AccountTypeIdType> accountTypeIdTypes
                = accountTypeIdTypeService.selectBypapersid(id);
        if (accountTypeIdTypes.size()!=0){
            return new Result(400,"删除失败，此数据不能删除",null);
        }else {
            int num = idTypeService.delIdType(id);
            if (num != 0) {
                return new Result(200, "删除成功", null);
            }
            return new Result(400, "删除失败", null);
        }
    }


    @RequestMapping("/selectByIdTypeId")
    public Object selectById(long id){
        IdType idType = idTypeService.selectById(id);
        if (idType!=null){
            return new Result(200,"查到数据",idType);
        }
        return new Result(400,"没有此数据数据",null);
    }


    @RequestMapping("/selectAllIdTypeAndZj")
    public Object selectAllIdTypeAndZj(@RequestParam(defaultValue = "1") int pagenum, int pagesize){
        PageInfo<IdType> pageInfo = idTypeService.selectAllIdTypeAndZj(pagenum, pagesize);
        if (pageInfo.getList().size()!=0){
            return new Result(200,"查询成功",pageInfo);
        }
        return new Result(400,"没有数据",null);
    }

}
